<?php
/*
	@ this is the only hard-coded include we got, everything else should load dynamically.
*/
include_once("config.php");

/*
	@ open files in our includes directory, that end with \".inc.php\"
*/
if(is_dir($_CMS['inc_dir'])) {
	if ($handle = opendir($_CMS['inc_dir'])) {
		while (false !== ($entry = readdir($handle))) {
			if ($entry != "." && $entry != "..") {
				$len = strlen($entry);
				if(substr($entry, $len-8, $len) === ".inc.php") {
					include_once($_CMS['inc_dir']."/".$entry);
				}
			}
		}
		closedir($handle);
	}
} else {
	trigger_error("please check config and ensure that the \"include directory\" is propery associated", E_USER_ERROR);
}

$doc = new HtmlDocument("path://template.php");
$doc->Find("div")->setHtml("<p>Test</p>");
echo $doc->getHtml();
die();

/*
	@ the magick register which will load our apps from a simple class name
		expected format would be AppModel where the directory is %root%/App/Model
*/
spl_autoload_register(function ($className) {
	global $_CMS;
	if(!class_exists($className, false)) {
		$words = wordsFromCompactString($className);
		$appName = array_shift($words);
		$modelName = array_shift($words);
		if(file_exists($_CMS["app_dir"]."/".$appName."/".$modelName.".php")) {
			include_once($_CMS["app_dir"]."/".$appName."/".$modelName.".php");
		}
	}
});

/*
	@ the standard for this CMS is that we capitalize the first letter for each app, model, action
		it is prettier, and allows us to define compact word collections e.g: HelloWorld can become Hello World
*/
$bootParams = array('app'=>'Default','mod'=>'Index','act'=>'Index','par'=>null);
if(isset($_GET['app'])) { $bootParams['app'] = $_GET['app']; }
if(isset($_GET['mod'])) { $bootParams['mod'] = $_GET['mod']; }
if(isset($_GET['act'])) { $bootParams['act'] = $_GET['act']; }
if(isset($_GET['par'])) { $bootParams['par'] = $_GET['par']; }

$appName = ucfirst(strtolower($bootParams['app']));
$modelName = ucfirst(strtolower($bootParams['mod']));
$actionName = ucfirst(strtolower($bootParams['act']));

$className = $appName.$modelName;
$classObject = new $className;

if(isset($_GET['nodisplay'])) {
	echo "<!-- no display -->";
}

/*
	@ note that all parameters which are passed in URL will be encoded in URL safe base64 and json for universal
		serialization.
*/
if($bootParams['par'] != null) {
/*
	@ if parameters do exist, we pass them to our models' action method
*/
	$classParameters = json_decode(base64_decode($bootParams['par']), true);
	call_user_func_array(array($classObject, $actionName), $classParameters);
} else {
	call_user_func(array($classObject, $actionName));
}

?>